package cn.echase.ecare.yinfayun.utils;


import cn.echase.common.core.utils.DateUtils;
import cn.echase.ecare.yinfayun.constants.AssessConstant;
import cn.echase.ecare.yinfayun.enums.EldIdTypeEnum;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;


@Slf4j
public class TransUtil {

    /**
     * 中国公民身份证号码最小长度。
     */
    private static final int CHINA_ID_MIN_LENGTH = 15;
    /**
     * 中国公民身份证号码最大长度。
     */
    private static final int CHINA_ID_MAX_LENGTH = 18;

    public static LocalDate getBirthDateFromId(String eldIdType, String eldIdNumber){
        if (ObjUtil.isNull(eldIdNumber)){
            return null;
        }

        // 身份证 + 长度15或18
        try {
            if (StrUtil.equalsAny(eldIdType, EldIdTypeEnum.TYPE_0.getTypeValue())){
                final int len = eldIdNumber.length();
                if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH){
                    DateTime birthDate = IdcardUtil.getBirthDate(eldIdNumber);
                    return DateUtils.dateToLocalDate(birthDate.toJdkDate());
                }
            }
        } catch (Exception e) {
            log.error("[getBirthDateFromId],根据证件号获取生日异常,eldIdType={},eldIdNumber={},errMsg={}", eldIdType, eldIdNumber, e.getMessage());
            return null;
        }
        return null;
    }

    public static Integer getAgeFromId(String eldIdType, String eldIdNumber){
        if (ObjUtil.isNull(eldIdNumber)){
            return null;
        }

        // 身份证 + 长度15或18
        try {
            if (StrUtil.equalsAny(eldIdType, EldIdTypeEnum.TYPE_0.getTypeValue())){
                final int len = eldIdNumber.length();
                if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH){
                    return IdcardUtil.getAgeByIdCard(eldIdNumber);
                }
            }
        } catch (Exception e) {
            log.error("[getAgeFromId],根据证件号获取生日异常,eldIdType={},eldIdNumber={},errMsg={}", eldIdType, eldIdNumber, e.getMessage());
            return null;
        }
        return null;
    }

    /**
     * 长者性别转换（从身份证号码中解析（性别(1 : 男 ， 0 : 女)），然后转换为这边定义的性（长者性别 0未填写 1 男 2 女））
     * 长者性别 0未填写 1 男 2 女
     */
    public static Integer getSexFromId(String eldIdType, String eldIdNumber){
        if (ObjUtil.isNull(eldIdNumber)){
            return 0;
        }

        // 身份证 + 长度15或18
        if (StrUtil.equalsAny(eldIdType, EldIdTypeEnum.TYPE_0.getTypeValue())){
            try {
                final int len = eldIdNumber.length();
                if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH){
                    int genderByIdCard = IdcardUtil.getGenderByIdCard(eldIdNumber);
                    if (IntegerUtil.equalsAny(genderByIdCard, 1)){
                        return AssessConstant.SexType.FEMALE;
                    }
                    if (IntegerUtil.equalsAny(genderByIdCard, 0)){
                        return AssessConstant.SexType.MALE;
                    }
                }
            } catch (Exception e) {
                log.error("[getSexFromId],根据证件号获取性别异常,eldIdType={},eldIdNumber={},errMsg={}", eldIdType, eldIdNumber, e.getMessage());
                return null;
            }
        }else {
            // 这种情况暂时不处理
            return null;
        }
        return 0;
    }

    public static Integer getSexFromId(String idNumber){
        return getSexFromId(EldIdTypeEnum.TYPE_0.getTypeValue(), idNumber);
    }

    /**
     * 长者性别 0未填写 1 男 2 女
     */
    public static String getSexStr(Integer eldSex){
        if (IntegerUtil.equalsAny(eldSex, AssessConstant.SexType.FEMALE)){
            return "男";
        }
        if (IntegerUtil.equalsAny(eldSex, AssessConstant.SexType.MALE)){
            return "女";
        }
        return "未填写";
    }

    /**
     * 评估状态：1: 未完成 2：已完成
     */
    public static String getAssessStatusStr(Integer assessStatus){
        if (IntegerUtil.equalsAny(assessStatus, AssessConstant.AssessStatus.UN_COMPLETE)){
            return "未完成";
        }
        if (IntegerUtil.equalsAny(assessStatus, AssessConstant.AssessStatus.COMPLETE)){
            return "已完成";
        }
        return "未知";
    }

    /**
     * 评估类型 0：首次评估  1：常规评估  2.即时评估  3：复评 4:其他
     */
    public static String getAssessTypeHomeStr(Integer assessType){
        if (IntegerUtil.equalsAny(assessType, AssessConstant.AssessTypeHome.TYPE_0)){
            return "首次评估";
        }
        if (IntegerUtil.equalsAny(assessType, AssessConstant.AssessTypeHome.TYPE_1)){
            return "常规评估";
        }
        if (IntegerUtil.equalsAny(assessType, AssessConstant.AssessTypeHome.TYPE_2)){
            return "即时评估";
        }
        if (IntegerUtil.equalsAny(assessType, AssessConstant.AssessTypeHome.TYPE_3)){
            return "复评";
        }
        if (IntegerUtil.equalsAny(assessType, AssessConstant.AssessTypeHome.TYPE_4)){
            return "其他";
        }
        return "未知";
    }

}
